﻿Imports TodoMicroSolutionPackV1.productos.dal.dto
Imports TodoMicroSolutionPackV1.dal.dao
Imports TodoMicroSolutionPackV1.dal.dto
Imports NHibernate
Imports NHibernate.Cfg
Public Class frmABMProducto
    Private prod As producto = New producto
    Private precio As precio = New precio
    Private _session As ISession
    Private DAOFactory As NHibernatedaofactory

    Protected ReadOnly Property Session As ISession
        Get
            If _session Is Nothing Then
                _session = NhibernateSessionFactory.getSession
            End If
            Return _session
        End Get
    End Property
    Private Sub btnNuevaEntidad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevaEntidad.Click
        prod = New producto
        blankFrm()
    End Sub

    Private Sub frmABMProducto_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If Not _session Is Nothing Then
            _session.Dispose()
        End If
    End Sub

    Private Sub frmABMProducto_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DAOFactory = New NhibernateDAOFactory(Session)
        comunes.populateCombobox(DAOFactory.getConstanteDAO.getConstantes("facturacion", "alicuota_iva"), "valor", "codigo", cmbIva)
        comunes.populateCombobox(DAOFactory.getConstanteDAO.getConstantes("facturacion", "unidades_medida"), "valor", "codigo", cmbUnidadMedida)
        comunes.populateCombobox(DAOFactory.getConstanteDAO.getConstantes("facturacion", "moneda"), "valor", "id", cmbMoneda)
    End Sub

    Private Sub btnEntidadExistente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEntidadExistente.Click
        prod = comunes.getProducto(Session)
        If Not prod Is Nothing Then
            productoToFrm()
            precio = DAOFactory.getPrecioDAO.getPrecioDefault(prod)
            cmbMoneda.SelectedIndex = cmbMoneda.FindStringExact(precio.moneda_nombre)
            txtPrecioNeto.Text = Math.Round(precio.precio_neto, 2)
            txtPrecioBruto.Text = Math.Round(precio.precio_neto * (CDbl(prod.iva.valor) / 100 + 1), 2)
        End If
    End Sub
    Private Sub blankFrm()
        txtId.Text = ""
        txtCodigo.Text = ""
        txtDescripcion.Text = ""
        txtPrecioNeto.Text = ""
        txtPrecioBruto.Text = ""
    End Sub
    Private Sub productoToFrm()
        txtId.Text = prod.id
        txtCodigo.Text = prod.codigo
        txtDescripcion.Text = prod.descripcion
        cmbIva.SelectedIndex = cmbIva.FindStringExact(prod.iva.valor)
        cmbUnidadMedida.SelectedIndex = cmbUnidadMedida.FindStringExact(prod.unidad_medida.valor)
    End Sub
    Private Sub frmToproducto()
        prod.codigo = txtCodigo.Text
        prod.descripcion = txtDescripcion.Text
        prod.iva = cmbIva.SelectedItem
        prod.unidad_medida = cmbUnidadMedida.SelectedItem
        precio.precio_neto = Math.Round(CDbl(txtPrecioNeto.Text), 2)
        precio.moneda = cmbMoneda.SelectedItem
        precio.lista_id = 0
    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        frmToproducto()
        Using tx As ITransaction = Session.BeginTransaction
            Session.SaveOrUpdate(prod)
            tx.Commit()
        End Using
        txtId.Text = prod.id
        MsgBox("Producto guardado con exito", MsgBoxStyle.Information, "TodoMicroSolutionPack")
    End Sub

    Private Sub txtPrecioNeto_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPrecioNeto.LostFocus
        If IsNumeric(txtPrecioNeto.Text) Then
            txtPrecioBruto.Text = Math.Round(CDbl(txtPrecioNeto.Text) * (CDbl(DirectCast(cmbIva.SelectedItem, constante).valor) / 100 + 1), 2)
        End If
    End Sub

    Private Sub txtPrecioBruto_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPrecioBruto.LostFocus
        If IsNumeric(txtPrecioBruto.Text) Then
            txtPrecioNeto.Text = Math.Round(CDbl(txtPrecioBruto.Text) / (CDbl(DirectCast(cmbIva.SelectedItem, constante).valor) / 100 + 1), 2)
        End If
    End Sub
End Class